<template>
	<view class="detail-page">
		<bnn-loading :loading="isLoading"/>
		<scroll-view scroll-y class="detail-body">
			<view class="time-line">
				<strategy-empty v-if="dataNull"/>
				<view class="timeline">
					<view v-for="(item, key) in list" :key="key" class="timeline-item">
						<view class="timeline-item-left">
							<view class="left-before-time">
								<view class="item-tag">买入</view>
							</view>
							<view class="operat-type">
								<view>
									
								</view>
							</view>
							<view class="left-time">{{item.created_at | parseTime('HH:mm:ss')}}</view>
						</view>
						<view class="timeline-item-axis">
							<view class="axis-circle"></view>
							<view class="axis-line"></view>
						</view>
						<view class="timeline-item-right">
							<view class="right-header d-f a-i-c">
								<view class="invest-type border">投机</view>
								<view class="strategy-name">{{item.strategy && item.strategy.name}}</view>
							</view>
							<view class="right-body">
								<view class="data-list">
									<view class="list-item">
										<view class="item-left">成交量：</view>
										<view class="item-right">{{item.deal_amount}}</view>
									</view>
									<view class="list-item">
										<view class="item-left">成交价格：</view>
										<view class="item-right">{{item.deal_price}}</view>
									</view>
									<view class="list-item">
										<view class="item-left">合约代码：</view>
										<view class="item-right">{{item.contract_code}}</view>
									</view>
									<view class="list-item">
										<view class="item-left">交易编码：</view>
										<view class="item-right">{{item.deal_code}}</view>
									</view>
									<view class="list-item">
										<view class="item-left">成交编号：</view>
										<view class="item-right">{{item.deal_number}}</view>
									</view>
								</view>
							</view>
							<view class="right-footer d-f a-i-c j-c-e">交易所：{{item.exchange}}</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {ACCESS_TOKEN} from '@/config/storage.js'
	import {parseTime} from '@/utils/index.js'
	import {toStrategyPayPage} from '@/utils/PageRouter.js'
	import BnnLoading from '@/components/bnn-ui/loading/index.vue'
	import StrategyEmpty from '@/components/data-empty/strategy.vue'
	
	export default {
		components: {BnnLoading, StrategyEmpty},
		data() {
			return {
				list: [],
				dataNull: false,
				count: 1,
				isLoading: true,
				intervalId: '',
			}
		},
		onLoad() {
			this.changeInfo()
		},
		//	home 按键事件
		onHide() {
			// uni.closeSocket()
			// uni.onSocketClose(function (res) {
			// 	uni.showToast({icon: 'none', title: 'onHide：WebSocket 已关闭！'});
			// })
		},
		//	返回其他页面触发
		onUnload() {
			const {intervalId} = this
			uni.closeSocket()
			if (intervalId) {
				clearInterval(intervalId)
			}
			// uni.onSocketClose(function (res) {
			// 	uni.showToast({icon: 'none', title: 'onUnload：WebSocket 已关闭！'});
			// })
		},
		methods: {
			// initData () {
			// 	count = 1
			// 	isLoading: true
			// },
			//	实时更新数据
			changeInfo () {
				const token = uni.getStorageSync(ACCESS_TOKEN)
				
				//	创建一个 WebSocket 连接。
				uni.connectSocket({
					url: 'ws://120.27.60.122:9501',
					header: {
						'content-type': 'application/json'
					}
				})
				
				//	监听WebSocket连接打开事件。
				uni.onSocketOpen(res => {
					let data = '{"action":"trade/login","content": {"token":"'+token+'"}}'
					uni.showToast({icon: 'none', title: '正在登录大厅'})
					uni.sendSocketMessage({data})
				})
				
				//	监听WebSocket错误。
				uni.onSocketError(res => {
					this.dataNull = true
					uni.showToast({icon: 'none', title: '连接失败'})
				})
				
				//	发送心跳
				this.intervalId = setInterval(() => {
					let data = '{"action":"trade/heartbeat","content": {"token":"'+token+'"}}'
					uni.sendSocketMessage({data})
				}, 60000)
				
				//	监听WebSocket接受到服务器的消息事件。
				uni.onSocketMessage(res => {
					const {count} = this
					const json = JSON.parse(res.data)
					const data = json.data || []
					data.map(item => {
						this.list.push(item)
					})
					
					if (count===2) {
						uni.hideLoading()
						if (data.length) {
							uni.showToast({icon: 'none', title: '正在处理数据'})
						} else {
							this.dataNull = !data.length
						}
					}
					
					if (data.length > 0) {
						this.dataNull = false
					}
					
					//	关闭加载层
					if (count===1 && this.isLoading) {
						setTimeout(() => {
							this.isLoading = false
							uni.showLoading({title: '加载中 . . .'})
						}, 500)
					}
					
					this.count = count+1
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.red{color: rgb(231, 76, 60) !important;}
	.gree{color: rgb(68, 184, 94) !important;}
	.echarts{
		width: 100%;
		height: 500rpx;
		background-color: #fff;
	}
	.detail-page{
		.detail-body{
			/* #ifdef APP-PLUS */
			height: 100vh;
			/* #endif */
			
			/* #ifndef APP-PLUS */
			height: calc(100vh - var(--window-top));
			/* #endif */
		}
		.proceeds{
			padding: 20rpx;
			background-color: #fff;
			margin-bottom: 20rpx;
			.header{
				height: 100rpx;
				display: flex;
				padding-bottom: 20rpx;
				.header-box{
					flex: 1;
					display: flex;
					align-items: center;
					flex-direction: column;
					.num{
						font-size: 28rpx;
						font-weight: bold;
						margin-bottom: 10rpx;
					}
					.name{
						color: #666;
						font-size: 24rpx;
					}
				}
			}
			.body{
				display: flex;
				flex-wrap: wrap;
				justify-content: space-between;
				.body-box{
					width: 320rpx;
					display: flex;
					margin-top: 10rpx;
					.name{
						width: 150rpx;
						color: #666;
						font-size: 24rpx;
					}
					.num{
						color: #333;
						font-size: 24rpx;
					}
				}
			}
		}
		
		.time-line{
			padding: 30rpx;
		}

		//	时间轴
		.timeline{
			display: flex;
			flex-direction: column-reverse;
			.timeline-item{
				display: flex;
				flex-wrap: nowrap;
				flex-direction: row;
				&:first-child{
					.axis-line{
						display: none;
					}
				}
			}
			.timeline-item-left{
				width: 110rpx;
				padding-bottom: 40rpx;
			}
			.left-before-time{
				margin-bottom: 10rpx;
			}
			.item-tag{
				color: #fff;
				width: fit-content;
				padding: 4rpx 10rpx;
				font-size: 24rpx;
				border-radius: 12rpx;
				background-color: $theme_color;
			}
			.left-time{
				color: #999;
				font-size: 24rpx;
			}
			.timeline-item-axis{
				margin: 0 30rpx;
				position: relative;
				.axis-circle{
					width: 16rpx;
					height: 16rpx;
					z-index: 10;
					border: 4rpx solid #a9bafc;
					border-radius: 50%;
					background-color: #3b6cee;
					position: absolute;
					top: 20rpx;
					left: 50%;
					transform: translate(-50%, 0);
				}
				.axis-line{
					width: 4rpx;
					height: 100%;
					z-index: 9;
					position: absolute;
					top: 20rpx;
					transform: translate(-50%, 0);
					background-color: #ebebeb;
					// border-left: 2px solid #e8e8e8;
				}
			}
			
			.timeline-item-right{
				flex: 1;
				border-radius: 10rpx;
				margin-bottom: 40rpx;
				background-color: #fff;
				.right-header{
					padding: 20rpx;
				}
				.invest-type{
					width: fit-content;
					padding: 4rpx 10rpx;
					font-size: 24rpx;
					margin-right: 20rpx;
					&::after{
						border-radius: 12rpx;
					}
				}
				.strategy-name{
					font-size: 32rpx;
					font-weight: bold;
				}
				
				.right-body{
					padding: 20rpx;
				}
				.right-footer{
					padding: 20rpx;
				}
			}
			
			.data-list{
				.list-item{
					display: flex;
					align-items: center;
					line-height: 48rpx;
					.item-left{
						width: 150rpx;
					}
					
					.item-right{
						
					}
				}
			}
		}
	}
</style>
